var util = require("../../utils/util");
var config = require("../../config");

var sliderWidth = 96; // 需要设置slider的宽度，用于计算中间位置

Page({
  data: {
    tabs: [
      "可借阅",
      "借阅中"
    ],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,

    inputShowed: false,
    inputVal: "",

    book_list: [],
    book_list2: [],

    page_index: 0,
    page_index2: 0,
    loading: false,
    isAll_1:false,
    isAll_2:false
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });
    that.getNewputaway();
    that.getNewborrow();
  },


  tabClick: function (e) {
    var that = this;
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });

    if (e.currentTarget.id == 0) {
      //that.getNewputaway();
    }
    else if (e.currentTarget.id == 1) {
      //that.getNewborrow();
    }
  },

  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.clearInput();
    this.setData({
      inputVal: "",
      inputShowed: false
    });
  },
  clearInput: function () {
    var that = this;
    this.setData({
      inputVal: "",
    });

    if (that.data.activeIndex == 0) {
      that.getNewputaway();
      this.setData({
        inputVal: "",
        book_list: []
      });
    }
    else {
      this.setData({
        inputVal: "",
        book_list2: []
      });
      that.getNewborrow();
    }
  },
  inputTyping: function (e) {
    var that = this;
    var kw = e.detail.value;
    this.setData({
      inputVal: kw
    });

    if (that.data.activeIndex == 0) {
      wx.request({
        url: config.urls.search_by_name_1,
        data: {
          name: kw,
          ps: 10,
          pn: 0
        },
        success: function (res) {
          console.log(res);
          if (res.data.success) {
            that.formatData(res.data.books);
            that.setData({
              book_list: res.data.books
            })
          }
          else {
            console.log("search by name 1, Book list get failed.");
          }
        }
      });
    }
    else {
      wx.request({
        url: config.urls.search_by_name_2,
        data: {
          name: kw,
          ps: 10,
          pn: 0
        },
        success: function (res) {
          console.log(res);
          if (res.data.success) {
            that.formatData(res.data.books);
            that.setData({
              book_list2: res.data.books
            })
          }
          else {
            console.log("search by name 2, Book list get failed.");
          }
        }
      });
    }
  },

  getNewputaway: function () {
    console.log("第一页")
    var that = this;
    // 获取已上架的书
    wx.request({
      url: config.urls.books_newputaway,
      data: {
        ps: 20,
        pn: that.data.page_index
      },
      success: function (res) {
        console.log(res);
        if (res.data.success) {
          if (res.data.books.length < 20){
            that.setData({
              isAll_1: true
            })
          }
          that.formatData(res.data.books);
          that.setData({
            book_list: that.data.book_list.concat(res.data.books),
            loading: false
          })
        }
        else {
          console.log("Book list get failed.");
        }
      }
    });
  },
  getNewborrow: function () {
    console.log("第二页")
    var that = this;
    // 获取已上架的书
    wx.request({
      url: config.urls.books_newborrow,
      data: {
        ps: 20,
        pn: that.data.page_index2
      },
      success: function (res) {
        console.log(res);
        if (res.data.success) {
          if (res.data.books.length < 20) {
            that.setData({
              isAll_2: true
            })
          }
          that.formatData(res.data.books);
          that.setData({
            book_list2: that.data.book_list2.concat(res.data.books),
            loading: false
          })
        }
        else {
          console.log("Book list get failed.");
        }
      }
    });
  },
  onPullDownRefresh: function () {
    console.log("下拉刷新")
    var that = this;
    if (that.data.activeIndex == 0) {
      that.setData({
        book_list: [],
        page_index: 0,
        isAll_1: false,
      });
      that.getNewputaway();
    }
    else {
      that.setData({
        book_list2: [],
        page_index2: 0,
        isAll_2: false,
      });
      that.getNewborrow();
    }
    wx.stopPullDownRefresh()
  },

  // onReachBottom: function () {
  //   console.log("上拉加载更多")
  //   var that = this;

  //   if (that.data.activeIndex == 0) {
  //     var books = that.data.book_list;
  //     that.data.page_index++;
  //     console.log(that.data.page_index)
  //     that.getNewputaway();
  //   }
  //   else if (that.data.activeIndex == 1) {
  //     if (!that.data.loading) {
  //       var books = that.data.book_list2;
  //       // 一次加载10个，如果是10的倍数才继续加载
  //       if (books.length > 0 && books.length % 10 == 0) {
  //         that.data.page_index2++;
  //         that.data.loading = true;
  //         that.getNewborrow();
  //       }
  //       else {
  //         // 否则不做任何事
  //         console.log("do nothing")
  //       }
  //     }
  //   }
  // },


  formatData: function (data) {
    for (var i = 0; i < data.length; i++) {
      var item = data[i];
      item.putaway_time = util.fds(item.putaway_time);
      if (item.borrower_name == "") {
        item.borrower_name = "无";
      }
      if (item.borrow_time == -1) {
        item.borrow_time = "无";
      }
      else {
        item.borrow_time = util.fds(item.borrow_time);
      }

    }
  },
  onShareAppMessage: function (res) {
    var that = this;
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '因书 - 借书、换书，因为书，而相识',
      path: that.route,
      success: function (res) {
        // 转发成功
        console.log(res.target)
      },
      fail: function (res) {
        // 转发失败
        console.log(res.target)
      }
    }
  },

  load_more:function(){
    console.log("加载更多")
    var that = this;
    if (that.data.activeIndex == 0) {
      var books = that.data.book_list;
      that.data.page_index++;
      that.getNewputaway();
    }
  },

  load_more_2:function(){
    console.log("加载更多")
    var that = this;
    if (that.data.activeIndex == 1) {
      var books = that.data.book_list;
      that.data.page_index2++;
      that.getNewborrow();
    }
  }
});
